<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 后台修改扫描记录
*/
class UpdateController extends CommonController{
	public function lightUp(){
		if (!$_POST['start_time'] || !$_POST['end_time'] || !$_POST['device_id']) {
			$this->ajaxReturn(array('status'=>0,'message'=>"参数错误"));
		}
		if($_POST['cate'] == 1){	//根据设备分类选择表
			$logModel = M('feichong_log');
		}elseif($_POST['cate'] == 3){
			$logModel = M('glue_log');
		}elseif ($_POST['cate'] == 4) {
			$logModel = M('mousetrap_log');
		}elseif ($_POST['cate'] == 5) {
			$logModel = M('reptile_log');
		}
		//开始和结束时间
		$start_time = strtotime(I('post.start_time'));
		$end_time = strtotime(I('post.end_time'));
		$device_id = I('post.device_id');
		$shop_id = M('device')->where('id='.$device_id)->getField('shop_id');
		$animals = I('post.animals');
		$where = array(
			'device_id' => $device_id,
			'animal_id' => 0,
			'add_time'	=> array('between',$start_time.','.$end_time),
		);
		
		foreach($animals as $key => $value) {
			if($_SESSION['type'] == 2)
				break;
			$diff = $value[2]-$value[1];
			if($diff == 0)
				continue;
			$where['animal_id'] = $value[0];
			$res = $logModel->where($where)->order('id desc')->find();
			//查询如果有记录的话就修改记录，没有记录的话就添加一条记录
			if($res){
				if($diff > 0){
					$logModel->where('id='.$res['id'])->setInc('number',$diff);
				}else{
					$diff = -1*$diff;
					$logModel->where('id='.$res['id'])->setDec('number',$diff);
				}
			}
			else{
				$addData = array(
					'number'	=> $diff,
					'device_id'	=> $device_id,
					'animal_id'	=> $value[0],
					'date_y'	=> intval(date('Y',$end_time-1)),
					'date_m'	=> intval(date('m',$end_time-1)),
					'shop_id'	=> $shop_id,
					'add_time'	=> $end_time-1
				);
				$logModel->add($addData);
			}
		}
		if($_POST['summary']){
			$summary = array(
				'device_id'	=> $device_id,
				'shop_id'	=> $shop_id,
				'date_y'	=> intval(date('Y',$end_time-1)),
				'date_m'	=> intval(date('m',$end_time-1)),
			);
			$zres = M('yue_zong')->where($summary)->find();
			if ($zres) {
				M('yue_zong')->where('id='.$zres['id'])->save(array('content'=>$_POST['summary']));
			}else{
				$summary['content'] = $_POST['summary'];
				M('yue_zong')->add($summary);
			}
			
		}
		
		$this->ajaxReturn(array('status'=>1));
	}


	//鼠饵修改
	public function baitUp(){
		if (!$_POST['start_time'] || !$_POST['end_time'] || !$_POST['device_id']) {
			$this->ajaxReturn(array('status'=>0,'message'=>"参数错误"));
		}
		$logModel = M('bait_log');
		//开始和结束时间
		$start_time = strtotime(I('post.start_time'));
		$end_time = strtotime(I('post.end_time'));
		$device_id = I('post.device_id');
		$shop_id = M('device')->where('id='.$device_id)->getField('shop_id');
		$animals = I('post.animals');
		$where = array(
			'device_id' => $device_id,
			'animal_id' => 0,
			'add_time'	=> array('between',$start_time.','.$end_time),
		);
		foreach($animals as $key => $value) {
			if($_SESSION['type'] == 2)
				break;
			$diff = $value[2]-$value[1];
			if($diff == 0)
				continue;
			$where['animal_id'] = $value[0];
			$res = $logModel->where($where)->order('id desc')->find();
			//查询如果有记录的话就修改记录，没有记录的话就
			if($res){
				if($diff > 0){
					$logModel->where('id='.$res['id'])->setInc('number',$diff);
				}else{
					$diff = -1*$diff;
					$logModel->where('id='.$res['id'])->setDec('number',$diff);
				}
			}
			else{
				$addData = array(
					'device_id'	=> $device_id,
					'number'	=> $diff,
					'date_y'	=> intval(date('Y',$end_time-1)),
					'date_m'	=> $value[0],
					'shop_id'	=> $shop_id,
					'add_time'	=> $end_time-1
				);
				$logModel->add($addData);
			}
		}

		//单个设备的总结添加
		if($_POST['summary']){
			$summary = array(
				'device_id'	=> $device_id,
				'shop_id'	=> $shop_id,
				'date_y'	=> intval(date('Y',$end_time-1)),
				'date_m'	=> intval(date('m',$end_time-1)),
			);
			$zres = M('yue_zong')->where($summary)->find();
			if ($zres) {
				M('yue_zong')->where('id='.$zres['id'])->save(array('content'=>$_POST['summary']));
			}else{
				$summary['content'] = $_POST['summary'];
				M('yue_zong')->add($summary);
			}
			
		}
	}
	//总体总结添加
	public function saveSum(){
		if(!$_POST){ $this->ajaxReturn(array('status'=>0,'message'=>'没有数据')); }
	    $data = I('post.');
	    $start_time = strtotime(I('post.start_time'));
		$end_time = strtotime(I('post.end_time'));
	    $dataArray = array(
	    	'shop_id' => $data['shop_id'],
	    	'dev_cate_id' => $data['cate'],
	    	'date_y'	=> intval(date('Y',$end_time)),
	    	'date_m'	=> intval(date('m',$end_time)),
	    );
	    $res = M('zong')->where($dataArray)->find();
	    if ($res) {
	    	M('zong')->where($dataArray)->save(array('content'=>$data['content']));
	    }else{
	    	$dataArray['content'] = $data['content'];
	    	M('zong')->add($dataArray);
	    }
	    $this->ajaxReturn(array('status'=>1,'message'=>'添加总结成功'));
	}
}